package com.hifar.test.equituit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.fw.utils.id.IdUtils;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;
import com.hifar.test.equip.pojo.VO.ResEquipUnitVO;
import com.hifar.test.equituit.mapper.HfResEquipUnitMapper;
import com.hifar.test.equituit.pojo.EquipUnitSearchVo;
import com.hifar.test.equituit.pojo.HfResEquipUnit;
import com.hifar.test.equituit.service.IHfResEquipUnitService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * <p>
 * 设备-项目-关闭表 服务实现类
 * </p>
 *
 * @author wangkepeng
 * @since 2024-12-18 14:14:04
 */
@Service
public class HfResEquipUnitServiceImpl extends ServiceImpl<HfResEquipUnitMapper, HfResEquipUnit> implements IHfResEquipUnitService {

	 
		@Override
		public BusiResponse exportExcel(Map<String, Object> pageInfo) {
		    BusiResponse busiResponse = new BusiResponse();
		    String ids = (String)pageInfo.get("ids");

		    String operationType = (String)pageInfo.get("operationType");

		    if (StringUtils.isNotBlank(operationType) && (operationType.equals("template"))){
		        ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfResEquipUnit.class, "导入模板", "设备-项目-关闭表导入数据");
		        return busiResponse;
		    }else {
		        QueryWrapper<HfResEquipUnit> postParam = RequestConver.getPostParam(pageInfo);
		        postParam.ne("del_flag", 1);
		        postParam.orderByDesc("create_time");
		        if (StringUtils.isNotBlank(ids)) {
		            postParam.in("id", ids);
		        }
		        List<HfResEquipUnit> dataList = this.list(postParam);
		         if(dataList!=null && dataList.size()>0) {
		        	  ExcelTemplateUtil.exportExcel(busiResponse, dataList, HfResEquipUnit.class, "设备-项目-关闭表数据", "设备-项目-关闭表导出");
		        }else {
		        	ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfResEquipUnit.class, "设备-项目-关闭表数据", "设备-项目-关闭表导出");
		        }
		        return busiResponse;
		    }

		}

		@Override
		@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
		public BusiResponse importExcel(MultipartFile file) throws Exception {
			if (file != null && !file.isEmpty()) {
				List<HfResEquipUnit> importExcel = ExcelTemplateUtil.importExcel(file.getInputStream(), HfResEquipUnit.class);
				List<HfResEquipUnit> importExcelList = new ArrayList<HfResEquipUnit>();
				if (importExcel != null && importExcel.size() > 0) {
					for (HfResEquipUnit bean : importExcel) {
						bean.setId(IdUtils.uuid32());
						//if(!bean.getName().equals("")) {
						importExcelList.add(bean);
						//}
					}
					this.saveBatch(importExcelList);
					return BusiResponse.ok("导入成功！");
				}
			}
			return BusiResponse.error("导入失败，原因未知！");
		}

	@Override
	public Page<EquipUnitSearchVo> selectEntityVoPage(Page<EquipUnitSearchVo> rowPage, QueryWrapper<EquipUnitSearchVo> wrapper) {
		return this.getBaseMapper().selectEntityVoPage(rowPage, wrapper);
	}

	@Override
	public List<EquipUnitSearchVo> listEntityVo(QueryWrapper<EquipUnitSearchVo> wrapper) {
		return this.getBaseMapper().listEntityVo(wrapper);
	}

	@Override
	public void removeByEquipId(String equipId) {
		QueryWrapper<HfResEquipUnit> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
				.eq(HfResEquipUnit::getEquipId, equipId);
		this.remove(queryWrapper);
	}

	@Override
	public List<ResEquipUnitVO> listByEquipId(String equipId) {
		return baseMapper.selectByEquipId(equipId);
	}
}
